package com.tencent.mavenproject1;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Hello world!
 *
 */
public class App 
{
    private Map<String, String> logLevelMap = new HashMap<String, String>();
    private String logLevel="info";
    private String keyWordRegExp="ActivityManager";
    
    public App() {
                logLevelMap.put("verbose", "V");
		logLevelMap.put("debug", "D");
		logLevelMap.put("info", "I");
		logLevelMap.put("warning", "W");
		logLevelMap.put("error", "E");
		logLevelMap.put("fatal", "F");
		logLevelMap.put("silent", "S");
    }
    
    public String getShortCutOfLogLevel(String fullLogLevelStr) {
		return this.logLevelMap.get(fullLogLevelStr);
	}
    
    
    public boolean hasDefinedLogLevelAndKeyWordOrReg(String log) {
		String level = getShortCutOfLogLevel(this.logLevel);
                System.out.println( level );
                System.out.println( log.indexOf(level) );
		if (-1<log.indexOf(level) && log.indexOf(level) <= 20) {// 有这个等级的log
			Pattern p = Pattern.compile(this.keyWordRegExp);
			Matcher m = p.matcher(log);
			return m.find();
		}
		return false;
	}
    
    public static void main( String[] args ) throws Exception {
//       String str1=" 01-21 17:41:01.424 I/ActivityManager(  390): Displayed com.android.packageinstaller/.UninstallAppProgress: +263ms";
//        
//        
//        System.out.println( "Hello World!" + str1.substring(0, str1.indexOf("/")-1).trim());
//        System.out.println( "Hello World!" + str1.substring(str1.indexOf("/")-1,str1.indexOf("/")));
//        System.out.println( "Hello World!" + str1.substring(str1.indexOf("/")+1,str1.indexOf("(")));
//        System.out.println( "Hello World!" + str1.substring(str1.indexOf("(")+1,str1.indexOf(")")));
//        System.out.println( "Hello World!" + str1.substring(str1.indexOf("):")+2,str1.length()));

        
//         InetSocketAddress addr = new InetSocketAddress("proxy.tencent.com", 8080);
//          // Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理
//          Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // http 代理
		
          
        Properties props = System.getProperties();
        props.setProperty("proxySet", "true");
        props.setProperty("socksProxyHost", "127.0.0.1");
        props.setProperty("socksProxyPort", "8888");
//        props.setProperty("mail.smtp.host", "smtp.163.com");
//        
       
        
        
        
    
                
                
                System.out.println( 1 );
                
               
                
                

                props.setProperty("mail.smtp.host", "smtp.163.com");
                props.put("mail.smtp.auth", "true");
                final String username_01 = "hujiangshui"; // 用户名
                final String password_01 = "hjs312net"; // 密码

//                Session newSession = Session.getInstance(props);
                //使用验证
                
                Session newSession = Session.getDefaultInstance(props);
       
                 System.out.println( 2 );
                
                MimeMessage message = new MimeMessage(newSession);
                InternetAddress from = new InternetAddress("hujiangshui@163.com"); // 发件人邮箱
                message.setFrom(from);
                InternetAddress sto = new InternetAddress("hujiangshui@163.com"); // 收件人邮箱
                message.setRecipient(Message.RecipientType.TO, sto);
                message.setRecipient(Message.RecipientType.CC, sto);
                message.setRecipient(Message.RecipientType.BCC, sto);
                System.out.println( 3 );
 
                message.setSentDate(new Date());
 
                
                
                message.setSubject("Test email #");
                message.setContent("This is test email # sent from Hudson Continuous Integration server.", "text/plain");
               
                
                System.out.println( 4 );
                
                Transport transport_01 = newSession.getTransport("smtp");
                transport_01.connect("hujiangshui", "hjs312net");
                
                System.out.println( 5);
                
                transport_01.send(message);

                System.out.println("Message sent.");
    
                
                
                
                
                
             
                
                
                
//                ///////////////////////
//                  URL url = new URL("http://www.baidu.com");
//            // 创建代理服务器
//            InetSocketAddress addr = new InetSocketAddress("proxy.tencent.com",8080);
//            // Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理
//            Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // http 代理
//            // 如果我们知道代理server的名字, 可以直接使用
//            // 结束
//            URLConnection conn = url.openConnection(proxy);
//            InputStream in = conn.getInputStream();
//            // InputStream in = url.openStream();
//           
//            System.out.println(in);
                
    
    }

   static class MyAuthenticator extends Authenticator {
        private String user = "";
        private String password = "";
        public MyAuthenticator(String user, String password) {
            this.user = user;
            this.password = password;
        }
        
        protected PasswordAuthentication getPasswordAuthentication() {
            return new javax.mail.PasswordAuthentication(user, password);
        }
    }

}
